//
//  StaticMethod.m
//  BJYShoppingMall
//
//  Created by Coder on 15/11/13.
//  Copyright © 2015年 BJY12. All rights reserved.
//

#import "StaticMethod.h"
#import "AppConfig.h"

@implementation StaticMethod

+(NSURL*)getImageUrlFromUrlString :(NSString*)origalUrl
{
    //"<img src=\"/clw/assets/up/adv/c1e9b422-70c8-47fb-a199-7f379b3a0f2d.jpg\" alt=\"\" />";
    
//    NSArray *array = [origalUrl componentsSeparatedByString:@"\""];
//    for (NSString *s in array) {
//        NSLog(@"%@",s);
//    }
//    if (array.count>2) {
//        return array[1];
//    }
//    return @"";
    
    return [NSURL URLWithString:[@"http://f.bjy12.com/" stringByAppendingString:origalUrl]];
    
}
+(NSString*)getAppVersion
{
    NSString * version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    
    return version;
}

+(NSString*)getAppBuild
{
    NSString * build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    
    return build;
}

+(float)autoSizeScaleX
{
    if(SCREEN_HEIGHT > 480){
        return SCREEN_WIDTH/320;
    }else{
        return 1.0;
    }
}

+(float)autoSizeScaleY
{
    if(SCREEN_HEIGHT > 480){
        return SCREEN_HEIGHT/568;
    }else{
        return 1.0;
    }
}

+(NSString *)phoneNumber
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *num = [defaults objectForKey:@"USER_PHONE_NUMBER"];
    return num;
}

+(void)setPhoneNumber:(NSString*)phone
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:phone forKey:@"USER_PHONE_NUMBER"];
    [defaults synchronize];
}

+(NSString *)areaCode
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *num = [defaults objectForKey:@"USER_AREA_CODE"];
    return num;
}

+(void)setAreaCode:(NSString*)area
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:area forKey:@"USER_AREA_CODE"];
    [defaults synchronize];
}
/**
 *  列如：http://app.bjy12.com/Activities/list?pcid=54
 *
 *  @param strURL 3G页面的链接URL
 *
 *  @return 截取pcid后的值
 */
+(NSString*)getPCIDformStrURL:(NSString *)strURL
{
    NSArray *strarray = [strURL componentsSeparatedByString:@"?"];
    return [strarray[1] substringFromIndex:5];
}

//返回订单状态
+(NSString*)getOrderState:(NSString *)OrderState OrderStateTex:(NSString*)OrderStateTex
{
    NSString * stateText;
    if ([OrderState isEqualToString:@"0"]) {
        
        stateText = @"用户未支付";
    }
    else if ([OrderState isEqualToString:@"1"]) {
        
        stateText = @"已确认";
    }
    else if ([OrderState isEqualToString:@"2"]) {
        
        stateText = @"仓库确认定单准备出库";
    }
    else if ([OrderState isEqualToString:@"3"]) {
        
        stateText = @"货物已出库";
    }
    else if ([OrderState isEqualToString:@"4"]) {
        
        stateText = @"交易完成";
    }
    else if ([OrderState isEqualToString:@"5"]) {
        
        stateText = @"待系统确认";
    }
    else if ([OrderState isEqualToString:@"998"]) {
        
        stateText = @"用户取消订单";
    }
    else if ([OrderState isEqualToString:@"997"]) {
        
        stateText = @"申请退货";
    }else if ([OrderState isEqualToString:@"996"]) {
        
        stateText = @"申请换货";
    }
    else if ([OrderState isEqualToString:@"995"]) {
        
        stateText = @"退货完成";
    }
    else if ([OrderState isEqualToString:@"994"]) {
        
        stateText = @"取消订单";
    }
    else if ([OrderState isEqualToString:@"993"]) {
        
        stateText = @"退款申请中";
    }
    else if ([OrderState isEqualToString:@"992"]) {
        
        stateText = @"退款处理中";
    }
    else if ([OrderState isEqualToString:@"991"]) {
        
        stateText = @"退款完成";
    }
    else if ([OrderState isEqualToString:@"999"]) {
        
        stateText = @"预约成功";
    }
    
    else if ([OrderState isEqualToString:@"889"]) {
        
        stateText = @"抢购失败";
    }
    else if ([OrderState isEqualToString:@"888"]) {
        
        stateText = @"支付超时";
    }else{
        stateText = OrderStateTex;
    }
    
    return stateText;
}
//上传图片前压缩处理图片
+ (NSData *)resetSizeOfImageData:(UIImage *)source_image maxSize:(NSInteger)maxSize
{
    //先调整分辨率
    CGSize newSize = CGSizeMake(source_image.size.width, source_image.size.height);
    
    CGFloat tempHeight = newSize.height / 1024;
    CGFloat tempWidth = newSize.width / 1024;
    
    if (tempWidth > 1.0 && tempWidth > tempHeight) {
        newSize = CGSizeMake(source_image.size.width / tempWidth, source_image.size.height / tempWidth);
    }
    else if (tempHeight > 1.0 && tempWidth < tempHeight){
        newSize = CGSizeMake(source_image.size.width / tempHeight, source_image.size.height / tempHeight);
    }
    
    UIGraphicsBeginImageContext(newSize);
    [source_image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //调整大小
    NSData *imageData = UIImageJPEGRepresentation(newImage,1.0);
    NSUInteger sizeOrigin = [imageData length];
    NSUInteger sizeOriginKB = sizeOrigin / 1024;
    if (sizeOriginKB > maxSize) {
        NSData *finallImageData = UIImageJPEGRepresentation(newImage,0.50);
        return finallImageData;
    }
    
    return imageData;
}


@end
